package com.goodving.guli.bt.profile.scale;

import android.bluetooth.BluetoothDevice;
import android.bluetooth.BluetoothGatt;
import android.bluetooth.BluetoothGattCallback;
import android.bluetooth.BluetoothGattCharacteristic;
import android.bluetooth.BluetoothGattDescriptor;
import android.bluetooth.BluetoothGattService;
import android.content.Context;

import java.util.ArrayList;
import java.util.UUID;
import java.util.concurrent.LinkedBlockingQueue;
import java.util.concurrent.ThreadPoolExecutor;
import java.util.concurrent.TimeUnit;

/**
 * Created by john on 2015/11/25.
 */
public class XMUUIDScanner { // C3901i
    public static final int f19624a = 0;
    public static final int mPoolSize = 1;
    public static final int f19626c = 2;
    private static ThreadPoolExecutor mThreadPoolExecutor; //f19627e
    private final String TAG;

    static {
        mThreadPoolExecutor = new ThreadPoolExecutor(mPoolSize, mPoolSize, 0, TimeUnit.MILLISECONDS, new LinkedBlockingQueue());
    }

    public XMUUIDScanner() {
        this.TAG = "UUIDScanner";
    }

    static class MyBluetoothGattCallback extends BluetoothGattCallback implements Runnable {
        private final String f19629a;
//        private final int f19630b;
        private Context mContext;
        private BluetoothDevice mBtDev;
        private C3895j f19633e;
        private ArrayList<UUID> f19634f;

        public MyBluetoothGattCallback(Context context, BluetoothDevice bluetoothDevice, C3895j c3895j) {
            this.f19629a = "UUIDTask";
//            this.f19630b = C3075a.f16147a;
            this.mContext = null;
            this.mBtDev = null;
            this.f19633e = null;
            this.f19634f = new ArrayList();
            this.mContext = context;
            this.mBtDev = bluetoothDevice;
            this.f19633e = c3895j;
        }

        private void m25393a(int i) {
            synchronized (this.mBtDev) {
                try {
                    this.mBtDev.wait((long) i);
                } catch (Exception e) {
                }
            }
        }

        private void stop() {
            synchronized (this.mBtDev) {
                this.mBtDev.notify();
            }
        }

        public void onConnectionStateChange(BluetoothGatt bluetoothGatt, int i, int i2) {
            GuliLog.m12370d("UUIDTask", "status:" + i + ",newState:" + i2 + ",clientIf:" + GattUtils.printClientIf(bluetoothGatt));
            if (i != 0) {
                stop();
            } else if (i2 == 2) {
                bluetoothGatt.discoverServices();
            }
        }

        public void onServicesDiscovered(BluetoothGatt bluetoothGatt, int i) {
            GuliLog.m12370d("UUIDTask", "status:" + i + ",clientIf:" + GattUtils.printClientIf(bluetoothGatt));
            if (i == 0) {
                for (BluetoothGattService bluetoothGattService : bluetoothGatt.getServices()) {
                    this.f19634f.add(bluetoothGattService.getUuid());
                    GuliLog.m12370d("UUIDTask", (bluetoothGattService.getType() == 0 ? "Primary" : "Secondary") + " service: " + GattUtils.m25341c(bluetoothGattService.getUuid()));
                    for (BluetoothGattCharacteristic bluetoothGattCharacteristic : bluetoothGattService.getCharacteristics()) {
                        GuliLog.m12370d("UUIDTask", "  Characteristic: " + GattUtils.m25341c(bluetoothGattCharacteristic.getUuid()));
                        GuliLog.m12370d("UUIDTask", "    - Properties: " + GattUtils.m25334b(bluetoothGattCharacteristic.getProperties()));
                        for (BluetoothGattDescriptor uuid : bluetoothGattCharacteristic.getDescriptors()) {
                            GuliLog.m12370d("UUIDTask", "    Descriptor: " + GattUtils.m25341c(uuid.getUuid()));
                        }
                    }
                }
            }
            stop();
        }

        public void run() {
            BluetoothGatt connectGatt = this.mBtDev.connectGatt(this.mContext, false, this);
            if (connectGatt != null) {
                m25393a(5000);//TBD m25393a(C3075a.f16147a);
                connectGatt.disconnect();
                connectGatt.close();
            }
            if (this.f19633e != null) {
                this.f19633e.m25374a(this.mBtDev, this.f19634f);
            }
        }
    }

    public static void m25392a( Context context, BluetoothDevice bluetoothDevice, C3895j c3895j) {

        mThreadPoolExecutor.execute(new MyBluetoothGattCallback(context, bluetoothDevice, c3895j));
    }
}
